﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SearchData
    {
        private readonly KeRui.DAL.SearchData dal = new KeRui.DAL.SearchData();

        public void Add(KeRui.Model.SearchData model)
        {
            this.dal.Add(model);
        }

        public List<KeRui.Model.SearchData> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.SearchData> list = new List<KeRui.Model.SearchData>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.SearchData item = new KeRui.Model.SearchData();
                    item.Tag = dt.Rows[i]["Tag"].ToString();
                    if (dt.Rows[i]["DataID"].ToString() != "")
                    {
                        item.DataID = int.Parse(dt.Rows[i]["DataID"].ToString());
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    item.Title = dt.Rows[i]["Title"].ToString();
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(string Tag, int DataID)
        {
            this.dal.Delete(Tag, DataID);
        }

        public List<KeRui.Model.SearchData> GetList(string strWhere, string strOrder, int PageSize, int PageIndex)
        {
            DataSet set = new KeRui.DAL.Common().GetList("SearchData", PageSize, PageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SearchData> GetListByCache(string strWhere, string strOrder, int PageSize, int PageIndex)
        {
            string cacheKey = string.Concat(new object[] { "SearchDataList-", strOrder, PageSize, PageIndex });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strWhere, strOrder, PageSize, PageIndex);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.SearchData>) cache;
        }

        public KeRui.Model.SearchData GetModel(string Tag, int DataID)
        {
            return this.dal.GetModel(Tag, DataID);
        }

        public KeRui.Model.SearchData GetModelByCache(string Tag, int DataID)
        {
            string cacheKey = "SearchDataModel-" + Tag + DataID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(Tag, DataID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SearchData) cache;
        }

        public void Update(KeRui.Model.SearchData model)
        {
            this.dal.Update(model);
        }
    }
}

